from django.shortcuts import render
from django.http import HttpRequest, HttpResponse, JsonResponse
from blog.models import Article
from django.core.paginator import Paginator
import json
from django.core import serializers



# Create your views here.
# request是http请求对象

def hello(request):
    return HttpResponse("hello")


def get_articles(request):
    article = Article.objects.all()[0]
    article_id = article.article_id
    article_title = article.article_title
    article_brief = article.article_brief
    article_date = article.article_date
    article_content = article.article_content
    res = "article_id:%s, article_title:%s, article_brief:%s, article_date:%s, article_content:%s"\
           % (article_id, article_title, article_brief, article_date, article_content)
    return HttpResponse(res)

    # articles = Article.objects.all()
    # response_data = {}
    # try:
    #     response_data['code'] = "200"
    #     response_data['message'] = "成功"
    #     response_data['data'] = serializers.serialize('json', articles)
    # except Exception as e:
    #     response_data['code'] = "500"
    #     response_data['message'] = "失败"
    # return HttpResponse(JsonResponse(res), content_type='application/json')


# 首页
def get_index(request):
    # 获取get请求的page参数
    curr_page = request.GET.get('page')
    prev_page = 0
    next_page = 0
    if curr_page:
        curr_page = int(curr_page)
    else:
        curr_page = 1

    articles = Article.objects.all()
    # 最近五条记录，根据时间排序
    recent_articles = Article.objects.order_by("-article_date")[:5]
    # django分页插件 传入文章，每页3条数据
    paginator = Paginator(articles, 3)

    numb_page = paginator.num_pages
    page_articles = paginator.page(curr_page)
    # 如果有下一页
    if page_articles.has_next():
        next_page = curr_page + 1
    else:
        next_page = curr_page

    if page_articles.has_previous():
        prev_page = curr_page - 1
    else:
        prev_page = 1
    content = {
        'articles': page_articles,
        'numb_page': range(1, numb_page + 1),
        'curr_page': curr_page,
        'next_page': next_page,
        'prev_page': prev_page,
        'recent_articles': recent_articles
    }
    return render(request, 'blog/index.html', content)


# 文章详情页面
def get_article(request, article_id):
    articles = Article.objects.all()
    # 当前文章
    curr_article = None
    # 上篇文章
    prev_index = 0
    prev_article = None
    # 下篇文章
    next_index = 0
    next_article = None
    for index, article in enumerate(articles):
        if index == 0:
            prev_index = 0
            next_index = index + 1
        elif index == len(articles) - 1:
            prev_index = index - 1
            next_index = index
        else:
            prev_index = index - 1
            next_index = index + 1
        if article.article_id == article_id:
            curr_article = article
            prev_article = articles[prev_index]
            next_article = articles[next_index]

    section_list = article.article_content.split("\n")
    content = {
        "section_list": section_list,
        "curr_article": curr_article,
        "prev_article": prev_article,
        "next_article": next_article
    }
    return render(request, 'blog/detail.html', content)


def add_article(request, article_title, article_brief, article_content):
    article = Article()
    article.article_title = article_title
    article.article_brief = article_brief
    article.article_content = article_content
    article.save()
    content = {
        'code': 200,
        'message': '添加成功'
    }
    return HttpResponse(json.dump(content))
    # return render(request, '', content_type=content)

